package com.yzt.zhuju.widget.pickview;

import android.content.Context;

import androidx.core.content.ContextCompat;

import com.yzt.zhuju.R;
import com.yzt.zhuju.widget.pickview.builder.OptionsPickerBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/12
 * desc   : 这是一个通用滑动管理类
 */
public class WhellPickerViewManager {

    private int index1;

    private int index2;

    private final List<String> mList1 = new ArrayList<>();

    private final List<List<String>> mList2 = new ArrayList<>();

    private final Context mContext;

    private OnCallOptionsListener listener;

    public interface OnCallOptionsListener {
        void callBackInfo(String sw1, String sw2);
    }

    public void setOnCallOptionsListener(OnCallOptionsListener listener) {
        this.listener = listener;
    }

    public WhellPickerViewManager(Context context) {
        this.mContext = context;
    }

    public static WhellPickerViewManager getInstance(Context context) {
        return new WhellPickerViewManager(context);
    }

    public WhellPickerViewManager builder(List<String> data) {
        mList1.clear();
        mList1.addAll(data);

        return this;
    }

    /**
     * 联动
     */
    public WhellPickerViewManager builder(List<String> data, List<List<String>> data2) {
        mList1.clear();
        mList1.addAll(data);

        mList2.clear();
        mList2.addAll(data2);

        return this;
    }

    public void setDefault(String sw1) {
        index1 = 0;
        for (int i = 0; i < mList1.size(); i++) {
            if (mList1.get(i).equals(sw1)) {
                index1 = i;
                break;
            }
        }
    }

    public void showPickerView(boolean isDialog) {// 弹出选择器
        //切换时是否还原，设置默认选中第一项。
        //设置选中项文字颜色
        OptionsPickerView pvOptionsPickerView = new OptionsPickerBuilder(mContext, (options1, options2, options3, v) -> {
            index1 = options1;
            index2 = options2;
            if (listener != null) {
                if (mList2.size() > 0) {
                    listener.callBackInfo(mList1.get(index1), mList2.get(index1).get(index2));
                } else {
                    listener.callBackInfo(mList1.get(index1), "");
                }
            }
        }).isRestoreItem(false)
                //切换时是否还原，设置默认选中第一项。
                .setSelectOptions(index1)
                .setLabels("", "", "")
                .setLineSpacingMultiplier(2.0f)
                .setContentTextSize(15)
                .setDividerColor(ContextCompat.getColor(mContext, R.color.color_bd2ffe))
                //设置选中项文字颜色
                .setTextColorCenter(ContextCompat.getColor(mContext, R.color.color_bd2ffe))
                .isDialog(isDialog)
                .setSubmitColor(ContextCompat.getColor(mContext, R.color.color_bd2ffe))
                .setCancelColor(ContextCompat.getColor(mContext, R.color.color_262628))

                .build();
        if (mList2.size() > 0) {
            //二级选择器
            pvOptionsPickerView.setPicker(mList1, mList2, null);
        } else {
            //二级选择器
            pvOptionsPickerView.setNPicker(mList1, null, null);
        }

        pvOptionsPickerView.show();
    }

    public void showPickerView() {
        showPickerView(false);
    }
}
